import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Book } from './book.entity';
import { Repository } from 'typeorm';

@Injectable()
export class BookService {
    constructor(
        @InjectRepository(Book) private readonly bookRepo: Repository<Book>
    ) { }

    //查询所有书籍
    async findAll(): Promise<Book[]> {
        return await this.bookRepo.find()

    }
    //查询所有书籍
    async findOneBook(id:string): Promise<Book> {
        return await this.bookRepo.findOne(id)

    }
    //添加书籍
    async createBook(book: Book): Promise<void> {
        await this.bookRepo.save(book)
    }
    //修改书籍
    async modifyBook(id: string, book: Book): Promise<void> {
        const existBook = await this.bookRepo.findOne(id)
        existBook.name = book.name
        existBook.price = book.price
        existBook.author = book.author
        this.bookRepo.save(existBook)
    }
    //删除书籍
    async deleteBook(id:string): Promise<void> {
        await this.bookRepo.delete(id)
    }
}
